'use strict';

/**
 * Config for the router
 */
angular.module('app')
    .run(
        ['$rootScope', '$state', '$stateParams','$cookies','$cookieStore','AccountService',
            function ($rootScope, $state, $stateParams,$cookies,$cookieStore,accountService) {
                $rootScope.$state = $state;   //方便获取状态
                $rootScope.$stateParams = $stateParams;    //方便获取状态

                $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState) {
                    if (toState.name == 'app.logout'){
                        accountService.logout().then(function(){
                            $state.go("access.signin", {from: fromState.name, w: 'logout'});
                        });
                    }
                    if (toState.name == 'access.signin' || toState.name == 'access.signup' || toState.name == 'access.forgotpwd' || toState.name == 'access.resetpwd' || toState.name =='access.clause')
                        return;// 如果是进入登录界面则允许
                    //请求异步导致下面执行出错，可优化,
                    //换成了jquery的
                    if (!$rootScope._useraccount) { //授权页面
                        if(($cookies["_login"] && $cookies["_login"] == '1') || $cookies["_rem_account"]){
                            accountService.getAccount(function(response){
                                if (response && response.code == 0) {
                                    $rootScope._useraccount = response.data;
                                    // TODO
                                    //主页的初始化消息
                                    $cookieStore.put('_login',1,{'expires': -1});
                                }
                            });
                        }
                    }
                    if($rootScope._useraccount){
                        if (toState.name == 'access.apply'){
                            return;
                        }
                        if($rootScope._useraccount.status == -1){
                            event.preventDefault();
                            $state.go("access.apply", {from: fromState.name});
                        }else if($rootScope._useraccount.status == 0){
                            event.preventDefault();
                            $state.go("access.signin", {from: fromState.name, w: 'notLogin'});
                        }else{
                            //主页的初始化消息
                            return; //可以正常访问
                        }
                    }else{
                        event.preventDefault();
                        $state.go("access.signin", {from: fromState.name, w: 'notLogin'});//跳转到登录界面
                    }


                    //to delete
                    /*if (!$rootScope._useraccount) { //授权页面
                        if(($cookieStore.get("_login") && $cookieStore.get("_login") == '1') || $cookieStore.get("_rem_account")){ //页面刷新的时候，记住登陆的时候
                            accountService.getAccount().then(function(response){ //请求当前用户，页面渲染
                                if (response.code == 0) {
                                    $rootScope._useraccount = response.data;
                                    $cookieStore.put('_login','1',{'expires': -1});
                                    if($rootScope._useraccount.isApply){
                                        if(!$rootScope._useraccount.storeId){
                                            event.preventDefault();
                                            $state.go("access.signin", {from: fromState.name, w: 'notLogin'});//跳转到登录界面
                                        }
                                    }else{
                                        event.preventDefault();
                                        $state.go("access.apply", {from: fromState.name});//跳转到审核页面
                                    }
                                }else{
                                    event.preventDefault();
                                    $state.go("access.signin", {from: fromState.name, w: 'notLogin'});
                                }
                            },function(fail){
                                event.preventDefault();
                            });
                        }else{
                            event.preventDefault();
                            $state.go("access.signin", {from: fromState.name, w: 'notLogin'});//跳转到登录界面
                        }
                    }else{
                        if($rootScope._useraccount.isApply){
                            if(!$rootScope._useraccount.storeId){
                                event.preventDefault();
                                $state.go("access.signin", {from: fromState.name, w: 'notLogin'});//跳转到登录界面
                            }
                        }else{
                            event.preventDefault();
                            $state.go("access.apply", {from: fromState.name});//跳转到审核页面
                        }
                    }*/
                });
            }
        ]
    )
    .config(
        ['$stateProvider', '$urlRouterProvider',
            function ($stateProvider, $urlRouterProvider) {

                $urlRouterProvider.otherwise('/app/home');
                $stateProvider
                    .state('app', {
                        abstract: true,
                        url: '/app',
                        templateUrl: 'tpl/app.html'  //app.html是搭建框架的，相当于sitemesh
                    })
                    .state('app.home', {
                        url: '/home',
                        templateUrl: 'tpl/home/home.html',
                        resolve: {
                            deps: ['$ocLazyLoad',
                                function ($ocLazyLoad) {
                                    return $ocLazyLoad.load(['js/controllers/home.js','js/services/baseinfo.js']);
                                }]
                        }
                    })
                    .state('app.orders', {
                        url: '/orders',
                        templateUrl: 'tpl/order/orders.html',
                        resolve: {
                            deps: ['$ocLazyLoad',
                                function ($ocLazyLoad) {
                                    return $ocLazyLoad.load(['js/controllers/orders.js','js/services/orders.js']);
                                }]
                        }
                    })

                    .state('app.goods', {
                        url: '/goods',
                        templateUrl: 'tpl/goods/goods.html',
                        resolve: {
                            deps: ['$ocLazyLoad',
                                function ($ocLazyLoad) {
                                    return $ocLazyLoad.load('ngFileUpload').then(function(){
                                        return $ocLazyLoad.load(['js/controllers/goods.js','js/services/goods.js']);
                                    });
                                }]
                        }
                    })
                    .state('app.accountinfo', {
                        url: '/account',
                        templateUrl: 'tpl/shop/accountinfo.html',
                        resolve: {
                            deps: ['$ocLazyLoad',
                                function ($ocLazyLoad) {
                                    return $ocLazyLoad.load(['js/controllers/accountinfo.js','js/services/store.js']);
                                }]
                        }
                    })
                    .state('app.cashExplain', {
                        url: '/cashExplain',
                        templateUrl: 'tpl/shop/cashExplain.html'
                    })
                    .state('app.logout', {
                        url: '/shop',
                        template: '<div ui-view class="fade-in-up"></div>'
                    })
                    .state('app.modifypwd',{
                        url: '/modifypwd',
                        templateUrl: 'tpl/access/page_modifypwd.html',
                        resolve: {
                            deps: ['$ocLazyLoad',
                                function ($ocLazyLoad) {
                                    return $ocLazyLoad.load(['js/controllers/modifypwd.js','js/services/account.js']);
                                }]
                        }
                    })
                    .state('app.shop', {
                        url: '/shop',
                        template: '<div ui-view class="fade-in-up"></div>'
                    })
                    .state('app.shop.baseinfo', {
                        url: '/baseinfo',
                        templateUrl: 'tpl/shop/baseinfo.html',
                        resolve: {
                            deps: ['$ocLazyLoad',
                                function ($ocLazyLoad) {
                                    return $ocLazyLoad.load('ngFileUpload').then(function(){
                                        return $ocLazyLoad.load(['js/services/baseinfo.js','js/controllers/baseinfo.js']);
                                    });
                                }]
                        }
                    })
                    .state('app.shop.statement', {
                        url: '/statement',
                        templateUrl: 'tpl/shop/statement.html',
                        resolve: {
                            deps: ['$ocLazyLoad',
                                function ($ocLazyLoad) {
                                    return $ocLazyLoad.load(['js/services/baseinfo.js','js/controllers/statement.js']);
                                }]
                        }
                    })
                    .state('app.shop.distribution', {
                        url: '/distribution',
                        templateUrl: 'tpl/shop/distribution.html',
                        resolve: {
                            deps: ['$ocLazyLoad',
                                function ($ocLazyLoad) {
                                    return $ocLazyLoad.load(['js/services/store.js','js/controllers/distribution.js','js/services/baseinfo.js']);
                                }]
                        }
                    })
                    .state('access', {
                        url: '/access',
                        template: '<div ui-view class="fade-in-right-big smooth"></div>'
                    })
                    .state('access.signin', {
                        url: '/signin',
                        templateUrl: 'tpl/access/page_signin.html',
                        resolve: {
                            deps: ['uiLoad',
                                function (uiLoad) {
                                    return uiLoad.load(['js/controllers/signin.js']);
                                }]
                        }
                    })
                    .state('access.signup', {
                        url: '/signup',
                        templateUrl: 'tpl/access/page_signup.html',
                        resolve: {
                            deps: ['uiLoad',
                                function (uiLoad) {
                                    return uiLoad.load(['js/controllers/signup.js']);
                                }]
                        }
                    })
                    .state('access.forgotpwd', {
                        url: '/forgotpwd',
                        templateUrl: 'tpl/access/page_forgotpwd.html',
                        resolve: {
                            deps: ['uiLoad',
                                function (uiLoad) {
                                    return uiLoad.load(['js/controllers/forgotpwd.js']);
                                }]
                        }
                    })
                    .state('access.resetpwd', {
                        url: '/resetpwd?{phone:1[345789][0-9]{9}}&code',//
                        templateUrl: 'tpl/access/page_resetpwd.html',
                        resolve: {
                            deps: ['uiLoad',
                                function (uiLoad) {
                                    return uiLoad.load(['js/controllers/resetpwd.js']);
                                }]
                        }
                    })
                    .state('access.clause', {
                        url: '/clause',
                        templateUrl: 'tpl/access/clause.html',
                        resolve: {
                            deps: ['uiLoad',
                                function (uiLoad) {
                                    return uiLoad.load(['js/controllers/signup.js']);
                                }]
                        }
                    })
                    .state('access.apply', {
                        url: '/apply',//{phone:[11]}&{code:[1-10]}
                        templateUrl: 'tpl/access/page_apply.html',
                        resolve: {
                            deps: ['$ocLazyLoad',
                                function ($ocLazyLoad) {
                                    return $ocLazyLoad.load('ngFileUpload').then(function(){
                                        return $ocLazyLoad.load(['js/services/baseinfo.js','js/controllers/apply.js']);
                                    });
                                }]
                        }
                    })
                    .state('access.404', {
                        url: '/404',
                        templateUrl: 'tpl/page_404.html'
                    })

                    // fullCalendar
                    .state('app.calendar', {
                        url: '/calendar',
                        templateUrl: 'tpl/app_calendar.html',
                        // use resolve to load other dependences
                        resolve: {
                            deps: ['$ocLazyLoad', 'uiLoad',
                                function ($ocLazyLoad, uiLoad) {
                                    return uiLoad.load(
                                        ['vendor/jquery/fullcalendar/fullcalendar.css',
                                            'vendor/jquery/fullcalendar/theme.css',
                                            'vendor/jquery/jquery-ui-1.10.3.custom.min.js',
                                            'vendor/libs/moment.min.js',
                                            'vendor/jquery/fullcalendar/fullcalendar.min.js',
                                            'js/app/calendar/calendar.js']
                                    ).then(
                                        function () {
                                            return $ocLazyLoad.load('ui.calendar');
                                        }
                                    )
                                }]
                        }
                    })

                    // mail
                    .state('app.mail', {
                        abstract: true,
                        url: '/mail',
                        templateUrl: 'tpl/mail.html',
                        // use resolve to load other dependences
                        resolve: {
                            deps: ['uiLoad',
                                function (uiLoad) {
                                    return uiLoad.load(['js/app/mail/mail.js',
                                        'js/app/mail/mail-service.js',
                                        'vendor/libs/moment.min.js']);
                                }]
                        }
                    })
                    .state('app.mail.list', {
                        url: '/inbox/{fold}',
                        templateUrl: 'tpl/mail.list.html'
                    })
                    .state('app.mail.detail', {
                        url: '/{mailId:[0-9]{1,4}}',
                        templateUrl: 'tpl/mail.detail.html'
                    })
                    .state('app.mail.compose', {
                        url: '/compose',
                        templateUrl: 'tpl/mail.new.html'
                    })

                    .state('layout', {
                        abstract: true,
                        url: '/layout',
                        templateUrl: 'tpl/layout.html'
                    })
                    .state('layout.fullwidth', {
                        url: '/fullwidth',
                        views: {
                            '': {
                                templateUrl: 'tpl/layout_fullwidth.html'
                            },
                            'footer': {
                                templateUrl: 'tpl/layout_footer_fullwidth.html'
                            }
                        },
                        resolve: {
                            deps: ['uiLoad',
                                function (uiLoad) {
                                    return uiLoad.load(['js/controllers/vectormap.js']);
                                }]
                        }
                    })
                    .state('layout.mobile', {
                        url: '/mobile',
                        views: {
                            '': {
                                templateUrl: 'tpl/layout_mobile.html'
                            },
                            'footer': {
                                templateUrl: 'tpl/layout_footer_mobile.html'
                            }
                        }
                    })
                    .state('layout.app', {
                        url: '/app',
                        views: {
                            '': {
                                templateUrl: 'tpl/layout_app.html'
                            },
                            'footer': {
                                templateUrl: 'tpl/layout_footer_fullwidth.html'
                            }
                        },
                        resolve: {
                            deps: ['uiLoad',
                                function (uiLoad) {
                                    return uiLoad.load(['js/controllers/tab.js']);
                                }]
                        }
                    })
                    .state('apps', {
                        abstract: true,
                        url: '/apps',
                        templateUrl: 'tpl/layout.html'
                    })
                    .state('apps.note', {
                        url: '/note',
                        templateUrl: 'tpl/apps_note.html',
                        resolve: {
                            deps: ['uiLoad',
                                function (uiLoad) {
                                    return uiLoad.load(['js/app/note/note.js',
                                        'vendor/libs/moment.min.js']);
                                }]
                        }
                    })
                    .state('apps.contact', {
                        url: '/contact',
                        templateUrl: 'tpl/apps_contact.html',
                        resolve: {
                            deps: ['uiLoad',
                                function (uiLoad) {
                                    return uiLoad.load(['js/app/contact/contact.js']);
                                }]
                        }
                    })
                    .state('app.weather', {
                        url: '/weather',
                        templateUrl: 'tpl/apps_weather.html',
                        resolve: {
                            deps: ['$ocLazyLoad',
                                function ($ocLazyLoad) {
                                    return $ocLazyLoad.load(
                                        {
                                            name: 'angular-skycons',
                                            files: ['js/app/weather/skycons.js',
                                                'vendor/libs/moment.min.js',
                                                'js/app/weather/angular-skycons.js',
                                                'js/app/weather/ctrl.js']
                                        }
                                    );
                                }]
                        }
                    })
                    .state('music', {
                        url: '/music',
                        templateUrl: 'tpl/music.html',
                        controller: 'MusicCtrl',
                        resolve: {
                            deps: ['$ocLazyLoad',
                                function ($ocLazyLoad) {
                                    return $ocLazyLoad.load([
                                        'com.2fdevs.videogular',
                                        'com.2fdevs.videogular.plugins.controls',
                                        'com.2fdevs.videogular.plugins.overlayplay',
                                        'com.2fdevs.videogular.plugins.poster',
                                        'com.2fdevs.videogular.plugins.buffering',
                                        'js/app/music/ctrl.js',
                                        'js/app/music/theme.css'
                                    ]);
                                }]
                        }
                    })
                    .state('music.home', {
                        url: '/home',
                        templateUrl: 'tpl/music.home.html'
                    })
                    .state('music.genres', {
                        url: '/genres',
                        templateUrl: 'tpl/music.genres.html'
                    })
                    .state('music.detail', {
                        url: '/detail',
                        templateUrl: 'tpl/music.detail.html'
                    })
                    .state('music.mtv', {
                        url: '/mtv',
                        templateUrl: 'tpl/music.mtv.html'
                    })
                    .state('music.mtvdetail', {
                        url: '/mtvdetail',
                        templateUrl: 'tpl/music.mtv.detail.html'
                    })
                    .state('music.playlist', {
                        url: '/playlist/{fold}',
                        templateUrl: 'tpl/music.playlist.html'
                    })
            }
        ]
    );